package cn.k.util;

import cn.k.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String EMPLOYEE_IN_SESSION="EMPLOYEE_IN_SESSION";
    public static final String EXPRESSIONS_IN_SESSION="EXPRESSIONS_IN_SESSION";

    //获取请求对象
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }


    //获取session对象
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    //往session存到登录员工对象
    public static void setEmployee(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    //往session得到登录员工对象
    public static Employee getEmployee(){
       return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }


    //从session存到权限表达式
    public static void setExpression(List<String> expression) {
        getSession().setAttribute(EXPRESSIONS_IN_SESSION,expression);
    }

    //从session取到权限表达式
    public static List<String> getExpression(){
        return (List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }
}













